﻿package fm.kgis.fm.service
{
	import com.kgis.flexutil.RemoteService;
	import com.kgis.flexutil.ServiceUtil;
	import com.kgis.flexutil.events.MethodReturnEvent;
	import com.kgis.mvc.CallbackUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	
	import mx.core.FlexGlobals;
	import mx.managers.BrowserManager;

	public class ServiceManager
	{
		private static var _instance:ServiceManager=null;

		public function ServiceManager(singleton:SingletonClass)
		{
			
			var contextPath:String=FlexGlobals.topLevelApplication.parameters.serverurl;
			if(!contextPath)
			{
				contextPath=ExternalInterface.call("BrowserHistory.getURL");
			}
		

			RemoteService.defaultChannelUri=ServiceUtil.getDefaultChannelUri(contextPath);
		}

		public static function onOperationReturn(eventDispatcher:EventDispatcher, event:MethodReturnEvent):void
		{
			var response:Event=null;
			response=CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
			eventDispatcher.dispatchEvent(response);
		}

		public static function getInstance():ServiceManager
		{
			if (_instance == null)
			{
				_instance=new ServiceManager(new SingletonClass());
			}

			return _instance;
		}
		private var _fmMetaDataManagementService:FmMetaDataManagementService = null;
		
		public static function getFmMetaDataManagementService():FmMetaDataManagementService {
			if (getInstance()._fmMetaDataManagementService == null) {
				getInstance()._fmMetaDataManagementService = new FmMetaDataManagementService();
			}
			return getInstance()._fmMetaDataManagementService;
		}
		
		private var _connectivityManagementService:ConnectivityManagementService = null;
		
		public static function getConnectivityManagementService():ConnectivityManagementService {
			if (getInstance()._connectivityManagementService == null) {
				getInstance()._connectivityManagementService = new ConnectivityManagementService();
			}
			return getInstance()._connectivityManagementService;
		}
		
		
		
		private var _commonManagementService:CommonManagementService = null;
		
		public static function getCommonManagementService():CommonManagementService {
			if (getInstance()._commonManagementService == null) {
				getInstance()._commonManagementService = new CommonManagementService();
			}
			return getInstance()._commonManagementService;
		}
		
		private var _fmEntityManagementService:FmEntityManagementService = null;
		
		public static function getFmEntityManagementService():FmEntityManagementService {
			if (getInstance()._fmEntityManagementService == null) {
				getInstance()._fmEntityManagementService= new FmEntityManagementService();
			}
			return getInstance()._fmEntityManagementService;
		}
	}
}

class SingletonClass
{
}
